package com.gouwudai.gobbs.listener;

import java.util.LinkedHashMap;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.apache.log4j.Logger;

import com.gouwudai.gobbs.pojo.TbbsRegCounter;
import com.gouwudai.gobbs.service.IRegCounterService;
import com.gouwudai.gobbs.vo.UserLoginVO;
import com.litt.core.common.BeanManager;

/**
 * <pre>
 *  Title:在线用户监听器
 *  Description: 在线用户监听器 
 * </pre>
 * 
 * @author <a href="mailto:littcai@hotmail.com">空心大白菜</a>
 * @date 2007-03-29
 * @version 1.0
 * <pre>
 * 修改记录
 *    修改后版本:     修改人：  修改日期:     修改内容: 
 * </pre>
 */
public class OnlineUserListener extends HttpServlet implements HttpSessionListener, HttpSessionAttributeListener 
{
	
	private static final Logger logger = Logger.getLogger(OnlineUserListener.class);
	
	private static final long serialVersionUID = 7734270500980966100L;
	
	public static int currentOnline = 0;	//当前在线人数，包括游客
	
	public static int mostOnline = 0;	//历史最高在线人数
	
	public static final LinkedHashMap<String,Object> onlineUserList = new LinkedHashMap<String,Object>(100);
	
	/**
	 * 
	 * 
	 * @param se
	 */
	public void sessionCreated(HttpSessionEvent se)
	{
		logger.info("创建了一个新连接...");
		currentOnline++;
		if(mostOnline==0)
		{	
			try
			{
				IRegCounterService regCounterService = (IRegCounterService)BeanManager.getBean("regCounterService");
		    	TbbsRegCounter tbbsRegCounter = regCounterService.loadTodayCounterInfo();
		    	logger.info("历史最高在线人数 - "+tbbsRegCounter.getMostOnline());	
		    	mostOnline = tbbsRegCounter.getMostOnline();
		    	if(currentOnline>mostOnline)	//当前在线人数大于历史最高在线人数
		    		mostOnline = currentOnline;
			}
			catch(Exception e)
			{
				logger.error(e);
			}
		}
		else
		{			
			if(currentOnline>mostOnline)	//当前在线人数大于历史最高在线人数
			{
				mostOnline = currentOnline;
				try
				{
					IRegCounterService regCounterService = (IRegCounterService)BeanManager.getBean("regCounterService");
					regCounterService.updateMostOnline(currentOnline);
				}
				catch(Exception e)
				{
					logger.error("更新历史最高在线人数失败",e);
				}
			}
			else
				mostOnline++;
		}					
	}
	
	/**
	 * @param se
	 */
	public void sessionDestroyed(HttpSessionEvent se)
	{
		currentOnline--;
		logger.info("注销了一个连接...");
	}	
	
    /**
     * 
     * 
     * @param hsbe
     */
    public void attributeAdded(HttpSessionBindingEvent hsbe)
    {
        if(logger.isDebugEnabled())
        {
            logger.debug("SESSION增加属性 - "+hsbe.getName());
        }   
        String sessionId = hsbe.getSession().getId();       
        UserLoginVO vo = null;
        if(hsbe.getName().equalsIgnoreCase("USER_LOGIN")&&onlineUserList.containsKey(sessionId))
        {
            vo =  (UserLoginVO)hsbe.getSession().getAttribute("USER_LOGIN");
            if(vo.getUserId()!=null)
                onlineUserList.put(sessionId,vo.getUserId());
        }   
        
    }

    /**
     * 
     * 
     * @param hsbe
     */
    public void attributeRemoved(HttpSessionBindingEvent hsbe)
    {
        if(logger.isDebugEnabled())
        {
            logger.debug("SESSION去除属性 - "+hsbe.getName());
        }
        String sessionId = hsbe.getSession().getId();
        UserLoginVO vo = null;
        if(hsbe.getName().equalsIgnoreCase("USER_LOGIN")&&onlineUserList.containsKey(sessionId))
        {           
            if(vo.getUserId()!=null)
                onlineUserList.remove(sessionId);
        }           
    }

    /**
     * 
     * 
     * @param hsbe
     */
    public void attributeReplaced(HttpSessionBindingEvent hsbe)
    {
        
    }   
	
	/**
	 * 
	 * 
	 * @return
	 */
	public static int getOnlineUserCount()
	{
		return onlineUserList.size();
	}
	
	/**
	 * 
	 * 
	 * @return
	 */
	public static int getCurrentOnline()
	{
		return currentOnline;
	}
	
	/**
	 * 
	 * 
	 * @return
	 */
	public static int getMostOnline()
	{
		return mostOnline;
	}

}